package edu.unindra.elearning.form.addin;

import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import usu.util.StringUtil;
import usu.widget.ButtonGlass;
import usu.widget.glass.PasswordBox;
import usu.widget.glass.TextBoxGlass;
import edu.unindra.elearning.model.DefaultOperator;
import edu.unindra.elearning.model.Operator;
import edu.unindra.elearning.util.PanelBackGround;
import edu.unindra.elearning.util.ViewPortGlass;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ELearningFormMainAdminOperatorAdd extends PanelBackGround {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JLabel jLabel6;

	private JTextArea textArea;

	private ButtonGlass btnSubmit;
	private ButtonGlass btnCancel;
	private ButtonGlass btnReset;

	private TextBoxGlass txtId;
	private TextBoxGlass txtName;
	private TextBoxGlass txtContact;
	private PasswordBox txtPassword;

	private JScrollPane jScrollPanel;
	private ViewPortGlass view;

	private String invalidMessage = "";

	public ELearningFormMainAdminOperatorAdd() {
		initComponents();
	}

	public String getInvalidMessage() {
		return invalidMessage;
	}

	public ButtonGlass getBtnSubmit() {
		return btnSubmit;
	}

	public ButtonGlass getBtnCancel() {
		return btnCancel;
	}

	private void initComponents() {
		jScrollPanel = new JScrollPane();
		view = new ViewPortGlass();

		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jLabel3 = new JLabel();
		jLabel4 = new JLabel();
		jLabel5 = new JLabel();
		jLabel6 = new JLabel();

		textArea = new JTextArea();

		txtId = new TextBoxGlass();
		txtName = new TextBoxGlass();
		txtContact = new TextBoxGlass();
		txtPassword = new PasswordBox();

		btnSubmit = new ButtonGlass();
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnCancel = new ButtonGlass();
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnReset = new ButtonGlass();

		view.setView(textArea);

		textArea.setColumns(20);
		textArea.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
		textArea.setForeground(new java.awt.Color(255, 255, 255));
		textArea.setLineWrap(true);
		textArea.setRows(5);
		textArea.setWrapStyleWord(true);
		textArea.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10,
				10, 10));
		textArea.setFocusAccelerator('A');
		textArea.setOpaque(false);

		setBorder(javax.swing.BorderFactory.createLineBorder(
				new java.awt.Color(0, 0, 0), 3));
		setMinimumSize(new java.awt.Dimension(485, 339));
		setPreferredSize(new java.awt.Dimension(485, 339));

		jLabel1.setDisplayedMnemonic('I');
		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
		jLabel1.setForeground(new java.awt.Color(255, 255, 255));
		jLabel1.setText("Id");

		jLabel2.setDisplayedMnemonic('P');
		jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
		jLabel2.setForeground(new java.awt.Color(255, 255, 255));
		jLabel2.setText("Password");

		jLabel3.setDisplayedMnemonic('N');
		jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
		jLabel3.setForeground(new java.awt.Color(255, 255, 255));
		jLabel3.setText("Nama");

		jLabel4.setDisplayedMnemonic('K');
		jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
		jLabel4.setForeground(new java.awt.Color(255, 255, 255));
		jLabel4.setText("Kontak");

		jLabel5.setDisplayedMnemonic('A');
		jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
		jLabel5.setForeground(new java.awt.Color(255, 255, 255));
		jLabel5.setText("Alamat");

		jScrollPanel.setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(0, 0, 0)));
		jScrollPanel.setOpaque(false);
		jScrollPanel.setViewport(view);

		btnSubmit.setMnemonic('T');
		btnSubmit.setText("Tambah");
		btnSubmit.setRoundRect(true);

		btnReset.setMnemonic('R');
		btnReset.setText("Reset");
		btnReset.setRoundRect(true);
		btnReset.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonResetActionPerformed(evt);
			}
		});

		btnCancel.setMnemonic('B');
		btnCancel.setText("Batal");
		btnCancel.setRoundRect(true);

		txtId.setForeground(new java.awt.Color(255, 255, 255));
		txtId.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		txtId.setFocusAccelerator('I');
		txtId.setFont(new java.awt.Font("Tahoma", 1, 11));

		txtPassword.setForeground(new java.awt.Color(255, 255, 255));
		txtPassword.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		txtPassword.setFocusAccelerator('P');
		txtPassword.setFont(new java.awt.Font("Tahoma", 1, 11));

		txtName.setForeground(new java.awt.Color(255, 255, 255));
		txtName.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		txtName.setFocusAccelerator('N');
		txtName.setFont(new java.awt.Font("Tahoma", 1, 11));

		txtContact.setForeground(new java.awt.Color(255, 255, 255));
		txtContact.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		txtContact.setFocusAccelerator('K');
		txtContact.setFont(new java.awt.Font("Tahoma", 1, 11));

		jLabel6.setFont(new java.awt.Font("Tahoma", 1, 24));
		jLabel6.setForeground(new java.awt.Color(255, 255, 255));
		jLabel6.setText("Tambah Operator Baru");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																				.addComponent(
																						jLabel5,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						65,
																						Short.MAX_VALUE)
																				.addComponent(
																						jLabel4,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						jLabel3,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						jLabel2,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						jLabel1))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						jScrollPanel,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						390,
																						Short.MAX_VALUE)
																				.addComponent(
																						txtId,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						390,
																						Short.MAX_VALUE)
																				.addComponent(
																						txtPassword,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						390,
																						Short.MAX_VALUE)
																				.addComponent(
																						txtName,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						390,
																						Short.MAX_VALUE)
																				.addComponent(
																						txtContact,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						390,
																						Short.MAX_VALUE)))
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addComponent(
																		btnReset,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		btnCancel,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		btnSubmit,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addComponent(
														jLabel6,
														javax.swing.GroupLayout.Alignment.TRAILING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														459, Short.MAX_VALUE))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(11, 11, 11)
								.addComponent(jLabel6)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel1)
												.addComponent(
														txtId,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel2)
												.addComponent(
														txtPassword,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel3)
												.addComponent(
														txtName,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel4)
												.addComponent(
														txtContact,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														jScrollPanel,
														javax.swing.GroupLayout.Alignment.TRAILING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														92, Short.MAX_VALUE)
												.addComponent(jLabel5))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														btnSubmit,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														btnReset,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														btnCancel,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap()));
	}

	private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {
		reset();
	}

	/**
	 * reset all text component
	 */
	public void reset() {
		textArea.setText("");
		txtContact.setText("");
		txtId.setText("");
		txtName.setText("");
		txtPassword.setText("");
	}

	public boolean isOperatorValid() {
		if (txtId.getText().trim().equalsIgnoreCase("")) {
			invalidMessage = "Id operator masih kosong";
			return false;
		}

		if (txtPassword.getPassword().length < 1) {
			invalidMessage = "Password masih kosong";
			return false;
		}

		if (txtName.getText().trim().equalsIgnoreCase("")) {
			invalidMessage = "Nama operator masih kosong";
			return false;
		}

		if (txtContact.getText().trim().equalsIgnoreCase("")) {
			invalidMessage = "No kontak masih kosong";
			return false;
		}

		if (textArea.getText().trim().equalsIgnoreCase("")) {
			invalidMessage = "Alamat masih kosong";
			return false;
		}
		return true;

	}

	public Operator getOperator() {
		DefaultOperator o = new DefaultOperator();
		o.setId(txtId.getText());
		o.setAddress(textArea.getText());
		o.setContact(txtContact.getText());
		o.setName(txtName.getText());
		o.setPassword(StringUtil.convertToString(txtPassword.getPassword()));

		return o;

	}
}
